package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.view.X5WebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity
 * @ClassName: WebPageActivity
 * @Description: web页面
 * @Author: ljp
 * @CreateDate: 2018/11/27 19:46
 * @Version: 1.0
 */
public class WebPageActivity extends BaseActivity implements UMShareListener {
    @BindView(R.id.web_view)
    X5WebView webView;
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private String url;
    private String title;
    private String userId;
    private String TAG = "WebPageActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        userId = getIntent().getStringExtra("userId");
        tvTitle.setText(title);
        webView.addJavascriptInterface(this, "android");
        if(userId != null){
            webView.loadUrl(url+"?&userId="+userId);
        }else {
            webView.loadUrl(url);
        }
        Log.i(TAG, "initLayout: ----------"+url+"&userId="+userId);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, final String s) {
                // websetting中设置的加载模式。 LOAD_DEFAULT：默认加载方式， 根据cache-control决定是否从网络上取数据
                webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
                //返回的是一个HitTestResult，一般会根据打开的链接的类型，返回的extra为null，或者没有返回extra。
                // 判断是否为null，可以用于解决网页重定向问题。
                WebView.HitTestResult hitTestResult = webView.getHitTestResult();
                //hitTestResult==null解决重定向问题
                Log.i(TAG, "shouldOverrideUrlLoading: ---webView----"+s);
                if (s != null) {
                    Intent intent = new Intent(activity, WebPageActivity.class);
                    intent.putExtra("url", s);
                    intent.putExtra("userId", userId);
                    if(s.startsWith("http:") || s.startsWith("https:")) {
                        if(s.substring(s.length()-4).equals("html")){
                            intent.putExtra("title", "规则");
                        }else {
                            Uri uri = Uri.parse(s);
                            intent = new Intent(Intent.ACTION_VIEW, uri);
                            startActivity(intent);
                        }
                        startActivity(intent);
                    }
                }
                return true;
            }
            @Override
            public void onPageFinished(WebView webView, String s) {

            }
        });

    }
    /*
     * 执行分享操作
     * */
    @JavascriptInterface
    public void doShare( String content,String title,String imageUrl,String url) {
        //Log.i(TAG, "doShare: content--"+content+"-title--"+title+"--imageUrl--"+imageUrl+"--url-"+url);
        UMWeb web = new UMWeb(url+"&userId="+userId);
        web.setTitle(title);//标题
        web.setThumb(new UMImage(activity, imageUrl));  //缩略图
        web.setDescription(content);//描述
        new ShareAction(activity).withMedia(web)
                .setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(this).open();

    }


    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * @descrption 分享开始的回调
     * @param platform 平台类型
     */
    @Override
    public void onStart(SHARE_MEDIA platform) {
    }
    /**
     * @descrption 分享成功的回调
     * @param platform 平台类型
     */
    @Override
    public void onResult(SHARE_MEDIA platform) {
        Toast.makeText(activity,"分享成功",Toast.LENGTH_LONG).show();
    }
    /**
     * @descrption 分享失败的回调
     * @param platform 平台类型
     * @param t 错误原因
     */
    @Override
    public void onError(SHARE_MEDIA platform, Throwable t) {
        Toast.makeText(activity,"分享失败"+t.getMessage(),Toast.LENGTH_LONG).show();
    }
    /**
     * @descrption 分享取消的回调
     * @param platform 平台类型
     */
    @Override
    public void onCancel(SHARE_MEDIA platform) {
        Toast.makeText(activity,"分享取消了",Toast.LENGTH_LONG).show();
    }
}
